home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Files / ParseFullPathname / ParseFullPathName.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.8 KB  |  118 lines  |  [TEXT/MPS ]

  1. /*
  2. **
  3. **        ParseFullPathname
  4. **
  5. **            Takes a *complete* full pathname and turns it into a DirID and VRefNum pair.
  6. **            This does not handle partial pathnames because it assumes that the first element
  7. **            in the pathname is volume name.
  8. **
  9. **            to recurse or not to recurse, that is the question... (not)
  10. **
  11. **            The #if immediately preceding the #include's allows you to use pre-compiled headers, speeding
  12. **            the compile-debug-compile cycle significantly.  after you compile this the first time, set the
  13. **            1 to a 0 to use the dump file...
  14. **
  15. **            Neil Day
  16. **            MacDTS
  17. **            Apple Computer, Inc.
  18. **
  19. */
  20.  
  21. #if 1
  22. #include <stdio.h>                                                            // Compiler Interfaces
  23. #include <files.h>
  24. #include <String.h>
  25. #include <Strings.h>
  26. #include <Memory.h>
  27. #include <Errors.h>
  28. #pragma dump "CDump"
  29. #else
  30. #pragma load "CDump"
  31. #endif
  32.  
  33. OSErr ParseFullPathname (FSSpec *Result, char *PathNamePtr);                // function prototypes
  34. Boolean GetElement (StringPtr Result,char * PathNamePtr,short ElementNumber);
  35.  
  36. main (int argc,char ** argv)
  37. {
  38.     OSErr        err;
  39.     FSSpec        here;
  40.     
  41.     if (argc != 2)
  42.         printf ("form: pfpn <pathname>\n");
  43.  
  44.     err = ParseFullPathname (&here, argv[1]);
  45.     if (err)
  46.         printf ("Died of error %d\n",err);
  47.     else
  48.         printf ("vRefNum %d DirID %d name %P\n",here.vRefNum, here.parID,here.name);
  49. }
  50.  
  51. Boolean GetElement (StringPtr Result,char *PathNamePtr,short ElementNumber)
  52. {
  53.     char             *eStart, *eEnd;
  54.     
  55.     eStart = eEnd = PathNamePtr;
  56.     
  57.     while (ElementNumber) {                                                    // Search for the element
  58.         if (*eEnd == ':' || !(*eEnd)) {                                        //   if we see colon or a null , we're at the end of element
  59.             --ElementNumber;                                                //   one down, n-1 to go
  60.             if (ElementNumber == 1)                                            //   are we at the second to last element??
  61.                 eStart = eEnd + 1;                                            //        mark it.
  62.         }
  63.         if (!(*eEnd)) break;
  64.         ++eEnd;                                                                //   always increment 
  65.     }
  66.     
  67.     if (ElementNumber || (eEnd - eStart > 32) || (eEnd - eStart == 0))        // If n > 0 or the element is too big or there is no element
  68.         return false;                                                         //  then croak.
  69.  
  70.     BlockMove ((Ptr) eStart, (Ptr) (Result + 1),                            // Move the substring into the Result
  71.         *Result = (char)eEnd - eStart);
  72.     
  73.     return true;
  74. }
  75.  
  76.  
  77. OSErr ParseFullPathname (FSSpec *Result,char *PathNamePtr)
  78. {
  79.     OSErr            err;
  80.     short            level = 2;
  81.     CInfoPBRec        dInfo;
  82.     HParamBlockRec    vInfo;
  83.  
  84.     Result->parID = fsRtDirID;                                                // start at the top of the volume
  85.  
  86.     if (GetElement ((StringPtr) &Result->name, PathNamePtr, 1)) {            // If there is a volume name
  87.         --(*Result->name);
  88.         vInfo.volumeParam.ioVRefNum =     0;                                    //   Get its vRefNum...
  89.         vInfo.volumeParam.ioNamePtr =    &Result->name;
  90.         vInfo.volumeParam.ioVolIndex =    -1;
  91.         err = PBHGetVInfo (&vInfo,false);
  92.         printf ("err %d\n",err);
  93.         if (err) return err;                                                //   and return an error if we couldn't find it
  94.         Result->vRefNum = vInfo.volumeParam.ioVRefNum;                        //   ...or put it into the Result
  95.     }
  96.  
  97.     while (GetElement ((StringPtr) &Result->name, PathNamePtr, level)) {    // Walk down the pathname
  98.         printf ("%P, %d, %c\n",Result->name, *Result->name, (Result->name + (*Result->name) -1 ));
  99.         if ((Result->name + (*Result->name-1)) == ':' ) {                        //   if the last char is a colon
  100.             --*(Result->name);                                                //   get rid of the colon on the end of the element
  101.             printf ("%P\n",Result->name);
  102.         }
  103.         dInfo.dirInfo.ioFDirIndex = 0;                                        //   and get it's DirID
  104.         dInfo.dirInfo.ioDrDirID = Result->parID;
  105.         dInfo.dirInfo.ioVRefNum = Result->vRefNum;
  106.         dInfo.dirInfo.ioNamePtr = &Result->name;
  107.         err = PBGetCatInfo (&dInfo,false);
  108.         if (err) return err;                                                //   if there was an error, return it
  109.         Result->parID = dInfo.dirInfo.ioDrDirID;                            //   otherwise, update the current DirID
  110.         ++level;                                                            //   go on to the next level
  111.     }
  112.     
  113.     printf ("what's left over: %s\n",PathNamePtr);
  114.  
  115.     
  116.     return noErr;                                                            // no problems
  117. }
  118.